package com.mediocre.controller;

import com.mediocre.service.IPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 随笔
 */
@Api(tags = "2 随笔管理")
@RestController
@Slf4j
@RequestMapping("/api")
public class PostController {

    @Autowired
    private IPostService postService;

    /**
     * query posts
     * @param p 第几页，如为空，则设为默认值1，必须为整型
     * @param cid 分类id，一般为""
     * @param tid 标签id，一般为""
     * @param t postType，文章类型：1 随笔；2 文章；3 日记。一般为"1"
     * @param cfg 一般为"0"
     * @param search 筛选随笔内容，若为空，表示不筛选
     * @param s 每页显示条数，如为空，则设为默认值10，也必须为整型，且只能在1（含）到100（含）的范围内取值，
     *          如果超出每页显示条数，即超过100，则返回错误消息： {"errors":["请求每页条数超出范围"],"type":0}
     * todo: @param orderBy=title%20asc
     * @return 查询结果，json字符串
     */
    @ApiOperation(value = "分页查询博客园的随笔", notes = "该筛选忽略英文大小写")
    @GetMapping("/posts/list")
    public String list(@RequestParam(name = "p") String p, @RequestParam(value = "cid", required = false) String cid,
                           @RequestParam(value = "tid", required = false) String tid, @RequestParam(value = "t", required = false) String t,
                           @RequestParam(value = "cfg", required = false) String cfg, @RequestParam(value = "search", required = false) String search,
                           @RequestParam("s") String s) {
        log.info("method list start");
        // eg. https://i.cnblogs.com/api/posts/list?p=1&cid=&tid=&t=1&cfg=0&search=pr&s=10
        String respJsonStr = postService.list(p, cid, tid, t, cfg, search, s);
        log.info("method list end");
        return respJsonStr;
    }

    /**
     * logically delete the target post
     * @param id
     * @return String
     */
    @ApiOperation("逻辑删除博客园的一篇随笔")
    @DeleteMapping("/posts/{id}")
    public String delete(@PathVariable("id") String id) {
        log.info("method delete start");
        // eg. https://i.cnblogs.com/api/posts/15155337
        String respJsonStr = postService.delete(id);
        log.info("method delete end");
        return respJsonStr;
    }

    /**
     * physically delete the target post
     * @param id
     * @return String
     */
    @ApiOperation("物理（彻底）删除博客园的一篇随笔")
    @DeleteMapping("/recycle-bin")
    public String deletePerm(@RequestParam("p") String id) {
        log.info("method deletePerm start");
        // eg. https://i.cnblogs.com/api/posts/15155337
        String respJsonStr = postService.deletePerm(id);
        log.info("method deletePerm end");
        return respJsonStr;
    }

    /**
     * create or update a post
     * @param postJsonStr
     * @return String
     */
    @ApiOperation("创建或新增博客园的一篇随笔")
    @PostMapping("/posts")
//    public String createOrUpdate(@RequestBody Post post) {
    public String createOrUpdate(@RequestBody String postJsonStr) {
        log.info("method createOrUpdate start");
        // eg. https://i.cnblogs.com/api/posts
        String respJsonStr = postService.createOrUpdate(postJsonStr);
        log.info("method createOrUpdate end");
        return respJsonStr;
    }
}
